<template>
  <div class="mod-seckill-seckill">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @refresh-change="refreshChange"
    >
      <!-- <template slot-scope="scope" slot="menuLeft"></template> -->
      <template slot-scope="scope" slot="status">
        <el-tag v-if="scope.row.status === 0" size="small" type="warning">{{
          $t("seckill.loseEffectiveness")
        }}</el-tag>
        <el-tag v-else-if="scope.row.status === 1" size="small">{{
          $t("publics.normal")
        }}</el-tag>
        <el-tag v-if="scope.row.status === 2" size="small" type="danger">{{
          $t("groups.offlineViolation")
        }}</el-tag>
        <el-tag v-if="scope.row.status === 3" size="small" type="warning">{{
          $t("coupon.checkPending")
        }}</el-tag>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          icon="el-icon-view"
          size="small"
          type="text"
          @click="addOrUpdateHandle(scope.row.seckillId)"
          >{{ $t("coupon.viewDetails") }}</el-button
        >
        <!-- <el-button type="text"
                   style="color:red"
                   icon="el-icon-bottom"
                   size="small"
                   v-if="scope.row.status === 1"
        @click="invalidHandle(scope.row.seckillId)">下线</el-button>-->
        <el-button
          type="text"
          icon="el-icon-bottom"
          size="small"
          v-if="isAuth('platform:seckill:update') && scope.row.status === 1"
          @click="offlineSeckillHandle(scope.row)"
          >{{ $t("coupon.offlineActivities") }}</el-button
        >
        <el-button
          type="text"
          icon="el-icon-document-checked"
          size="small"
          v-if="isAuth('platform:seckill:update') && scope.row.status > 1"
          @click="seckillAuditHandle(scope.row.seckillId)"
          >{{
            scope.row.status === 2
              ? $t("coupon.violationOffShelf")
              : $t("coupon.checkPending")
          }}</el-button
        >
        <el-button
          type="text"
          icon="el-icon-delete"
          size="small"
          style="color: red"
          :disabled="scope.row.status > 1"
          @click="deleteHandle(scope.row.seckillId)"
          >{{ $t("coupon.delete") }}</el-button
        >
      </template>
    </avue-crud>
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="refreshChange"
    ></add-or-update>

    <!-- 商品审核弹窗 -->
    <SeckillAudit
      v-if="seckillAuditVisible"
      ref="seckillAudit"
      selectUrl="/platform/seckill/getOfflineHandleEventBySeckillId"
      updateUrl="/platform/seckill/auditSeckill"
      @refreshDataList="refreshChange"
    ></SeckillAudit>
  </div>
</template>

<script>
import { tableOption } from '@/crud/marketing/seckill'
import AddOrUpdate from './seckill-add-or-update'
import SeckillAudit from '@/components/offline-audit'
export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {},
      seckillAuditVisible: false,
      dataListLoading: false,
      tableOption: tableOption,
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate,
    SeckillAudit
  },
  created () {
  },
  mounted () {
  },
  methods: {
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/platform/seckill/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize
            },
            this.searchForm
          )
        )
      }).then(({ data }) => {
        data.records.forEach(element => {
          if (element.maxNum === -1) {
            element.maxNum = this.$i18n.t('seckill.unlimited')
          }
        })
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    deleteHandle (id) {
      this.$confirm(this.$i18n.t('seckill.isDeleOper'), this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('coupon.confirm'),
        cancelButtonText: this.$i18n.t('coupon.cancel'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/platform/seckill/' + id),
          method: 'delete',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList(this.page)
            }
          })
        })
      }).catch(() => { })
    },
    invalidHandle (id) {
      this.$confirm(this.$i18n.t('seckill.invalidateActivity'), this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('coupon.confirm'),
        cancelButtonText: this.$i18n.t('coupon.cancel'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/platform/seckill/invalid/' + id),
          method: 'put',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => { })
    },
    // 刷新回调用
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.searchForm = params
      this.getDataList(this.page)
    },
    // 下线活动
    offlineSeckillHandle (row) {
      // '确定对活动[' + row.seckillName + ']进行下线操作？如是，请填写下线理由'
      var tips = this.$i18n.t('seckill.offlineTip')
      tips = tips.replace('ACTIVITYNAME', row.seckillName)
      this.$prompt(tips, this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('coupon.confirm'),
        cancelButtonText: this.$i18n.t('coupon.cancel')
      }).then((obj) => {
        if (!obj.value) {
          this.$message.error(this.$i18n.t('seckill.offlineReasonTip'))
          return
        }
        this.$http({
          url: this.$http.adornUrl(`/platform/seckill/offline`),
          method: 'post',
          data: this.$http.adornData({
            handleId: row.seckillId,
            offlineReason: obj.value || null
          })
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList(this.page)
            }
          })
        })
      })
    },
    // 弹窗处理
    seckillAuditHandle (id) {
      this.seckillAuditVisible = true
      this.$nextTick(() => {
        this.$refs.seckillAudit.init(id)
      })
    }
  }
}
</script>
<style lang="scss">
.mod-seckill-seckill {
}
</style>
